<template>
      <client-only>
    <div class="main">
      <div class="title">
        <a class="active" href="/login">登录</a>
        <span>·</span>
        <a href="/register">注册</a>
      </div>
  
      <div class="sign-up-container">
        <el-form :model="user" :rules="rules" ref="userForm">
  
          <el-form-item class="input-prepend restyle" prop="mobile">
            <div >
              <el-input type="text" placeholder="手机号" v-model="user.mobile"/>
              <i class="iconfont icon-phone" />
            </div>
          </el-form-item>
  
          <el-form-item class="input-prepend" prop="password">
            <div>
              <el-input type="password" placeholder="密码" v-model="user.password"/>
              <i class="iconfont icon-password"/>
            </div>
          </el-form-item>
  
          <div class="btn">
            <input type="button" class="sign-in-button" value="登录" @click="submitForm">
          </div>
        </el-form>
        <!-- 更多登录方式 -->
        <div class="more-sign">
          <h6>社交帐号登录</h6>
          <ul>
            <li><a id="weixin" class="weixin" target="_blank" href="javascript:;"><i class="iconfont icon-weixin"/></a></li>
            <li><a id="qq" class="qq" target="_blank" href="javascript:;"><i class="iconfont icon-qq"/></a></li>
          </ul>
        </div>
      </div>
    </div>
</client-only>
  </template>
  
  <script>
    import '~/assets/css/sign.css'
    import '~/assets/css/iconfont.css'
    import cookie from 'js-cookie'
    import memberApi from '/api/member'
    export default {
      layout: 'sign',
      data(){
        var validateMobile=(rule,value,callback)=>{
            if (/^1[3456789]\d{9}$/.test(value)) {
                //验证通过
                callback()
            }else{
                callback(new Error('手机号码格式不正确'))
            }
        }
        return{
             //实现手机号码自定义验证规则
       
            user:{
                mobile:'',//手机号
                password:'',//密码
            },
            rules: {//表单验证规则
                mobile: [
                { required: true, message: '请输入手机号码', trigger: 'blur' },//trigger: 'blur'触发时机
                { validator:this.validateMobile, trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },   
                ],
            }
        }
      },
      methods:{
       //提交注册表单
       submitForm(){
            // 对表单内容进行校验
            this.$refs['userForm'].validate((valid)=>{
                if(valid){
                    // 验证通过
                    memberApi.login(this.user).then(res=>{                
                       //登陆成功，获取token信息，将token进行本地存储方便下次访问使用
                       cookie.set('member_token',res.data.token)
                        //获取当前用户信息
    
                    }).then(()=>{
                      memberApi.getInfo().then(res=>{        
                            console.log(res.data);
                            //将用户基本信息存入cookie中，以便后期使用
                            let userInfo=JSON.stringify(res.data)
                            //console.log(userInfo);
                            cookie.set('member_info',userInfo)
                             // 跳转至登陆页面
                            this.$router.push('/')
                        })  
                      }).catch(err=>{
                      console.log(err);
                    })
                }else{
                    // 格式校验失败
                    console.log("err submit!!!");
                    return false;
                }
            })
        },
      }
    }
  </script>
  <style>
     .el-form-item__error{
      z-index: 9999999;
    }
  </style>